//
//  HJZStatusCommentToolBar.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/6/2.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "HJZStatusCommentToolBar.h"

#define HJZButtonPadings 10

@interface HJZStatusCommentToolBar ()

@property (nonatomic, weak) UIButton *retweetBtn;
@property (nonatomic, weak) UIButton *commentBtn;
@property (nonatomic, weak) UIButton *unlikeBtn;
@property (nonatomic, weak) UIView *moveView;
@property (nonatomic, strong) NSMutableArray *frames;



@end

@implementation HJZStatusCommentToolBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        // 添加三个按钮
        [self setupBtns];
       

    }
    return self;
}


- (NSMutableArray *)frames {
    if (_frames == nil) {
        _frames = [[NSMutableArray alloc] init];
    }
    return _frames;
}

/**
 *  设置按钮
 */
- (void)setupBtns {
    self.retweetBtn = [self setupBtnWithTitle:@"转发"];
    self.retweetBtn.tag = 2000;
    self.commentBtn = [self setupBtnWithTitle:@"评论"];
    self.commentBtn.tag = 2001;
    self.unlikeBtn = [self setupBtnWithTitle:@"赞"];
    self.unlikeBtn.tag = 2002;
    [self.commentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
}



/**
 *  自定义set方法
 */
- (void)setStatus:(HJZStatus *)status
{
    _status = status;
    [self setupBtn:self.retweetBtn count:status.reposts_count text:@"转发"];
    [self setupBtn:self.commentBtn count:status.comments_count text:@"评论"];
    [self setupBtn:self.unlikeBtn count:status.attitudes_count text:@"赞"];
    [self setupMoveView];
}

/**
 *  设置Button内容
 */
- (void)setupBtn:(UIButton *)btn count:(int)count text:(NSString *)text
{
    NSString *title = nil;
    if (count < 10000) {
        title = [NSString stringWithFormat:@"%@ %d", text, count];
    } else {
        title = [NSString stringWithFormat:@"%@ %.1f万", text, count / 10000.0];
        title = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }

    NSDictionary *btnArrtibutes = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};
    CGFloat width = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:btnArrtibutes context:nil].size.width;
    [self.frames addObject:@(width)];
   
    [btn setTitle:title forState:UIControlStateNormal];
}

/**
 *  添加按钮
 */
- (UIButton *)setupBtnWithTitle:(NSString *)title
{
    UIButton *btn = [[UIButton alloc] init];
    [btn setTitle:title forState:UIControlStateNormal];

    [btn setTitleColor:COLOR(133, 133, 133, 1) forState:UIControlStateNormal];


    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self addSubview:btn];
    
 
    
    return btn;
}



/**
 *  设置移动的视图
 */
- (void)setupMoveView {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor orangeColor];
    [self addSubview:view];
    self.moveView = view;
    
    CGRect moveRect = (CGRect){10 + [_frames[0] integerValue] + 10, 29,[_frames[1] integerValue],2};
    self.moveView.frame = moveRect;
}

/**
 *  点击方法
 */
- (void)buttonAction:(UIButton *)button {
    
    [self.retweetBtn setTitleColor:COLOR(133, 133, 133, 1) forState:UIControlStateNormal];
    [self.commentBtn setTitleColor:COLOR(133, 133, 133, 1) forState:UIControlStateNormal];
    [self.unlikeBtn setTitleColor:COLOR(133, 133, 133, 1) forState:UIControlStateNormal];
    NSInteger flag = button.tag - 2000;
    if (flag == 0) {
        [self.retweetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.1 animations:^{
            self.moveView.frame = (CGRect){10 , 29,[_frames[0] integerValue],2};
        }];
        
    }else if (flag == 1) {
        [self.commentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.1 animations:^{
            self.moveView.frame = (CGRect){10 + [_frames[0] integerValue] + 10, 29,[_frames[1] integerValue],2};
        }];
    }else {
        [self.unlikeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.1 animations:^{
            self.moveView.frame = (CGRect){kWidth - 10 - [_frames[2] integerValue], 29,[_frames[2] integerValue],2};
        }];
    
    }
    
    if ([self.delegate respondsToSelector:@selector(statusCommentToolBarClickWithButtonTag:)]) {
        [self.delegate statusCommentToolBarClickWithButtonTag:flag];
    }
    

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect retweetRect = (CGRect){10,0,[_frames[0] integerValue], 30};
    CGRect commentRect = (CGRect){10 + [_frames[0] integerValue] + 10,0,[_frames[1] integerValue],30};
    CGRect likeRect = (CGRect){kWidth - 10 - [_frames[2] integerValue],0,[_frames[2] integerValue], 30};
    self.retweetBtn.frame = retweetRect;
    self.commentBtn.frame = commentRect;
    self.unlikeBtn.frame =likeRect;
    
    
}


@end
